﻿var Comment = function () {
    lstcomment = [];
    lstcommentsend = [];
    function loadtable(namkh, initData){
        $.ajax({
            url: initData.GetallCommentByDv,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            data: {
                'namkh': namkh,
                'moc': 3,
                'flag':1
            },
            async: false,
            success: function (data) {
                $('#body').empty();
                if (data.length > 0 && data !="err") {
                    lstcomment = [];
                    $('#Create').prop('disabled', false);
                    $('#gridContent').show();
                    $.each(data, function (i, v) {
                        var comment = v.COMMENT_BAN;
                        var row = '<tr class="webgrid-row-style">\
                                        <td class="clsSTT" style="text-align: center; width: 30px;">\
                                            '+ (i + 1) + '\
                                        </td>\
                                        <td>\
                                            <div id="spnFullName" class="display-mode">\
                                                <span class="hidden-780 tenbc">' + $('#Namlapbc option:selected').text() + '</span>\
                                            </div>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">' + v.TEN_CHI_TIEU + '</span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                    <label title="' + v.COMMENT_BAN + '">\
                                                        ' +  comment+ '\
                                                </label>\
                                            </span>\
                                        </td>\
                                        <td class="clsUserName hidden-480">\
                                            <span id="spnUserName" class="display-mode timeline">\
                                                ' + v.DON_VI + '\
                                            </span>\
                                        </td>\
                                    </tr>';
                        $('#body').append(row);
                        lstcomment.push(v);
                    });
                } else {
                    $('#Create').prop('disabled', true);
                    $('#gridContent').hide();
                }
                           
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
    }
    var initLoadData = function (initData) {
        $.ajax({
            url: initData.GetallNamkehoach,
            type: "GET",
            headers: {
                'VerificationToken': initData.VerificationToken
            },
            dataType: "json",
            async: false,
            success: function (data) {
                var lst = [];
                $('#Namlapbc').empty();
                $('#Namlapbc').append('<option value="-1">[--Chọn năm kế hoạch--]</option>');
                $.each(data, function (i, v) {
                    if (v.STATUS_ID >= 40) {
                        $('#Namlapbc').append('<option value="' + v.NAM_KE_HOACH_ID + '">' + v.NAM_KE_HOACH + '</option>');
                        lst.push(v);
                    }
                });
                var namkh = $.cookie('mkhidchung');
                if (namkh != undefined) {
                    $('#Namlapbc').val(namkh);
                    loadtable(namkh, initData);
                }
                data = lst;
            },
            error: function (jqXHR, textStatus, errorThrown) {
                window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
            },
            complete: function () {
            }
        });
    };
    var initButtonClick = function (initData) {
        $('#Create').on('click', function () {
            $('#bd').empty();
            $.each(lstcomment, function (i, v) {
                var dc = "";
                var thuchien = "";
                if (v.DieuChinh != null) {
                    dc = v.DieuChinh;
                }
                if (v.ThucHien != null) {
                    thuchien = v.ThucHien;
                }
                $('#bd').append('<tr>\
                                <td>' + v.TEN_CHI_TIEU + '</td>\
                                 <td>' + v.DON_VI_TINH + '</td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" class="e-input1 form-control" level="1" value="' + v.KeHoachDauNam + '" /></td>\
                                <td><input type="text" disabled="disabled"  chitieuid="' + v.CHI_TIEU_ID + '" class="e-input1 form-control" level="2" value="' + dc + '" /></td>\
                                <td><input type="text"   chitieuid="' + v.CHI_TIEU_ID + '" class="e-input3 form-control" level="3" value="' + thuchien + '" /></td>\
                                <td>'+v.COMMENT_BAN+'</td>\
                                <td><input type="button" class="btn btn-info reject"  chitieuid="' + v.CHI_TIEU_ID + '"  data-html="true" data-placement="bottom" level="12" title="Nội dung comment"  value="Ý kiến" /></td>\
                            </tr>');
            });
            $('#myModal').modal('show');
        });
        $('.reject').live('click', function () {
            $(this).popover({
                trigger: 'manual'
            });


            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var contentnew = '<div class="row">\
            <div class="col-md-12">\
                <textarea class="form-control comment" chitieuid="' + chitieu + '" level="13" rows="6" ></textarea>\
            </div>\
            </div>\
            <br />\
            <div class="row">\
            <center>\
                <button type="button" chitieuid="' + chitieu + '"   class="btn ghinhan">Ghi nhận</button>\
                <button type="button" chitieuid="' + chitieu + '"  class="btn huybo">Huỷ</button>\
            </center>\
            </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.reject').live('hide.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var giatri = $('[chitieuid="' + chitieu + '"][level="3"]').val();
            var comment = $('[chitieuid="' + chitieu + '"][level="13"]').val();
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu) {
                    lstcommentsend.splice(i, 1);
                    return false;
                }
            });
            if (comment.trim() != "") {
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': '',
                    'DON_VI_ID': initData.DonviID,
                    'GIA_TRI': giatri,
                    'COMMENT_DV': comment,
                    'STATUS_ID': 1,
                    'IS_ACTIVE': 1,
                    'MOC': 3,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lstcommentsend.push(obj);
                $('input[chitieuid="' + chitieu + '"][level="12"]').val("Warning");
                $('input[chitieuid="' + chitieu + '"][level="12"]').removeClass("btn-danger");
                $('input[chitieuid="' + chitieu + '"][level="12"]').addClass("btn-warning");
            } else {
                $('input[chitieuid="' + chitieu + '"][level="12"]').val("Ý kiến");
                $('input[chitieuid="' + chitieu + '"][level="12"]').removeClass("btn-warning");
                $('input[chitieuid="' + chitieu + '"][level="12"]').addClass("btn-info");
            }
        });
        $('.reject').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var comment = "";
            $.each(lstcommentsend, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu) {
                    comment = v.COMMENT_DV;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][level="13"]').text(comment);
        });
        $('.commnetban').live('click', function () {
            $(this).popover({
                trigger: 'manual'
            });
            if ($(this).hasClass('pop')) {
                $(this)
                    .popover('hide')
                    .removeClass('pop');
            } else {
                var chitieu = $(this).attr('chitieuid');
                var contentnew = '<div class="row">\
                <div class="col-md-12">\
                    <textarea class="form-control comment" chitieuid="' + chitieu + '"level="10" rows="8" ></textarea>\
                </div>\
                </div>';
                $(this)
                    .attr('data-content', contentnew)
                    .popover('show')
                    .addClass('pop');
            }

        });
        $('.commnetban').live('shown.bs.popover', function () {
            var chitieu = $(this).attr('chitieuid');
            var comment = "";
            $.each(lstcomment, function (i, v) {
                if (v.CHI_TIEU_ID == chitieu) {
                    comment = v.COMMENT_BAN;
                    return false;
                }
            });
            $('[chitieuid="' + chitieu + '"][level="10"]').text(comment);
        })
        $('#Phanhoi').live('click', function () {
            if (lstcommentsend.length == 0) {
                window.notice('Không có ý kiến cho đơn vị nào.', window.notice_error);
            } else {
                $.ajax({
                    url: initData.UpdateCommentbyDV,
                    type: "POST",
                    headers: {
                        'VerificationToken': initData.VerificationToken
                    },
                    dataType: "json",
                    data: JSON.stringify({
                        'cmt': lstcommentsend,
                    }),
                    contentType: 'application/json',
                    success: function (data) {
                        if (data != 0) {
                            window.notice('Bạn đã gửi phản hồi tới ban thành công !', window.notice_success);
                            loadtable($('#Namlapbc').val(), initData);
                            $('#myModal').modal('hide');
                        } else {
                            window.notice('Bạn đã gửi phản hồi tới ban thất bại !', window.notice_error);
                        }
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        window.notice('Có lỗi xảy ra trong quá trình lưu thông tin: ' + errorThrown, window.notice_error);
                    },
                    complete: function () {
                    }
                });
            }
        });
        $('input[level="3"]').live('change', function () {
            var chitieu = $(this).attr('chitieuid');
            var data = $(this).val();
            var kq = false;
            $.each(lstcommentsend, function (i) {
                if (lstcommentsend[i].CHI_TIEU_ID === chitieu) {
                    lstcommentsend[i].GIA_TRI = data;
                    kq = true;
                    return false;
                }
            });
            if (kq == false) {
                var obj = {
                    'COMMENT_ID': 0,
                    'BAN_ID': 0,
                    'CHI_TIEU_ID': chitieu,
                    'COMMENT_BAN': '',
                    'DON_VI_ID': initData.DonviID,
                    'GIA_TRI': $(this).val(),
                    'COMMENT_DV': '',
                    'STATUS_ID': 1,
                    'IS_ACTIVE': 1,
                    'MOC': 3,
                    'NAM_KE_HOACH_ID': $('#Namlapbc').val()
                }
                lstcommentsend.push(obj);
                $('input[chitieuid="' + chitieu + '"][level="12"]').val("Warning");
                $('input[chitieuid="' + chitieu + '"][level="12"]').removeClass("btn-danger");
                $('input[chitieuid="' + chitieu + '"][level="12"]').addClass("btn-warning");
            }

        })
        $('.ghinhan').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            $('[chitieuid="' + chitieu + '"][level="12"]').trigger('click');
        });
        $('.huybo').live('click', function () {
            var chitieu = $(this).attr('chitieuid');
            $('[chitieuid="' + chitieu + '"][level="13"]').val('');
            $('[chitieuid="' + chitieu + '"][level="12"]').trigger('click');
        });
        $('#Namlapbc').on('change',function(){
            var data = $(this).val();
            if (data != -1) {
                loadtable(data, initData);
            }
            $.cookie('mkhidchung', $(this).val());

        })
    }
    return {
        init: function (initData) {
            //load data
            initLoadData(initData);
            initButtonClick(initData);
        }
    }
}();
